package com.ruoyi.project.loans.loansLog.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.PaymentUtil;
import com.ruoyi.project.loans.loansLog.domain.LoansLog;
import com.ruoyi.project.loans.loansLog.mapper.LoansLogMapper;
import com.ruoyi.project.loans.payment.domain.PaymentDetail;
import com.ruoyi.project.loans.payment.mapper.PaymentDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.text.Convert;

/**
 * 贷款记录 服务层实现
 * 
 * @author ruoyi
 * @date 2020-06-17
 */
@Service
public class LoansLogServiceImpl implements ILoansLogService 
{
	@Autowired
	private LoansLogMapper loansLogMapper;

	@Autowired
	private PaymentDetailMapper paymentDetailMapper;

	/**
     * 查询贷款记录信息
     * 
     * @param id 贷款记录ID
     * @return 贷款记录信息
     */
    @Override
	public LoansLog selectLoansLogById(Integer id)
	{
	    return loansLogMapper.selectLoansLogById(id);
	}
	
	/**
     * 查询贷款记录列表
     * 
     * @param loansLog 贷款记录信息
     * @return 贷款记录集合
     */
	@Override
	public List<LoansLog> selectLoansLogList(LoansLog loansLog)
	{
	    return loansLogMapper.selectLoansLogList(loansLog);
	}
	
    /**
     * 新增贷款记录
     * 
     * @param loansLog 贷款记录信息
     * @return 结果
     */
	@Override
	public int insertLoansLog(LoansLog loansLog)
	{
		// 1.新增贷款记录
		int result = loansLogMapper.insertLoansLog(loansLog);
		// 2.生成还款明细
		if(result > 0){
			// 贷款编号
			Integer id = loansLog.getId();
			// 贷款金额
			BigDecimal loanAmounts = loansLog.getLoanAmounts();
			//贷款多久
			Integer loanHowLong = loansLog.getLoanHowLong();
			// 还款方式
			Integer paymentMethod = loansLog.getPaymentMethod();
			// 贷款时间
			Date loanTime = loansLog.getLoanTime();
			//借款利率
			BigDecimal loanRate = loansLog.getLoanRate();
			//还款日
			Integer paymentDay = loansLog.getPaymentDay();

			// 等额本息
			if (paymentMethod == 0){
				PaymentDetail[] paymentDetails = PaymentUtil.debxPayment(loanAmounts, loanRate, new BigDecimal(loanHowLong), loanTime,paymentDay);

				for(int i=0;i<loanHowLong;i++){
					PaymentDetail repay=paymentDetails[i];
					repay.setLoanId(id);
					paymentDetailMapper.insertPaymentDetail(repay);
			    }

			}else if(paymentMethod == 1){
				//先息后本
				PaymentDetail[] paymentDetails = PaymentUtil.xxhbPayment(loanAmounts, loanRate, new BigDecimal(loanHowLong), loanTime,paymentDay);


				for(int i=0;i<loanHowLong;i++){
					PaymentDetail repay=paymentDetails[i];
					repay.setLoanId(id);
					paymentDetailMapper.insertPaymentDetail(repay);
				}

			}else if (paymentMethod == 2){
				//等额本金
				PaymentDetail[] paymentDetails = PaymentUtil.debjPayment(loanAmounts, loanRate, new BigDecimal(loanHowLong), loanTime,paymentDay);

				for(int i=0;i<loanHowLong;i++){
					PaymentDetail repay=paymentDetails[i];
					repay.setLoanId(id);
					paymentDetailMapper.insertPaymentDetail(repay);
				}
			}
		}
	    return result;
	}
	
	/**
     * 修改贷款记录
     * 
     * @param loansLog 贷款记录信息
     * @return 结果
     */
	@Override
	public int updateLoansLog(LoansLog loansLog)
	{
	    return loansLogMapper.updateLoansLog(loansLog);
	}

	/**
     * 删除贷款记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteLoansLogByIds(String ids)
	{


		int result = loansLogMapper.deleteLoansLogByIds(Convert.toStrArray(ids));
		if(result > 0){
			result = paymentDetailMapper.deletePaymentDetailByLoanIds(Convert.toStrArray(ids));
		}
		return result;
	}
	
}
